// pages/mail2/mail2.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },
 
  title: function (res) {
    app.globalData.title = res.detail.value
    this.setData({
      title: res.detail.value
    })
  },
  content: function (res) {
    app.globalData.content = res.detail.value
    this.setData({
      content: res.detail.value
    })
  },
  radiochange: function (res) {
    app.globalData.original = res.detail.value
    this.setData({
      original: res.detail.value
    })
  },
  submit: function () {
    if(getApp().globalData.title==null){
      wx.showModal({
        showCancel: false,
        content: '请填写标题！',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    else if(getApp().globalData.content==null){
      wx.showModal({
        showCancel: false,
        content: '请填写内容！',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    else{
      wx.showModal({
        content: '确认提交 ？',
        success (res) {
          if (res.confirm) {
             //获取当前时间
   var timestamp = Date.parse(new Date());
   var timestamp = timestamp / 1000;
   var n = timestamp * 1000;
   var date = new Date(n);
   var Y = date.getFullYear();  
   var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);   
   var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();  
   var h = date.getHours();  
   var m = date.getMinutes();  
   var s = date.getSeconds();  
   console.log("当前时间：" ,Y+"/"+M+"/"+D+" "+h+":"+m+":"+s); 
            console.log('用户点击确定')
            const db = wx.cloud.database()
            db.collection("mail")
              .add({
                data: {
                  original:getApp().globalData.original,
                  author : getApp().globalData.author,
                  contact : getApp().globalData.contact,
                  music :getApp().globalData.music,
                   word :getApp().globalData.word,
                   title:getApp().globalData.title,
                   content:getApp().globalData.content,
                   selectcontactIndex:getApp().globalData.selectcontactIndex,
                   time:Y+"/"+M+"/"+D+" "+h+":"+m+":"+s,
                   state:"未看"
                },
                success: function () {
                  app.globalData.author=null
                  app.globalData.contact=null
                  app.globalData.music=null
                  app.globalData.title=null
                  app.globalData.word=null
                  app.globalData.content=null
                  wx.showToast({
                    title: '提交成功',
                    duration: 2000
                  }),
                  wx.navigateTo({
                    url: '../index2/index2'
                  })
                }
              })     
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      author : getApp().globalData.author,
     contact : getApp().globalData.contact,
     music :getApp().globalData.music,
      word :getApp().globalData.word,
      title:getApp().globalData.title,
      content:getApp().globalData.content,
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})